<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>用户管理</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>

<body>
	<%@ include file="/WEB-INF/jsp/common/header.jsp"%>

	<script type="text/javascript">
		$(function() {
			bindtree();
			bindgrid();
			binduserdept();
			bindcombogrid();
		});
	</script>
	<script>
		function del(node) {
			$.ajax({
				type : "post",
				url : "UserManage/DelUser",
				//                dataType: "xml",
				data : {
					userid : node.userid,
					czr : PUser[0].loginname
				},
				success : function(data) {
					$.messager.alert('消息', data);
					$('#add').window('close');
					bindgrid();
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					$.messager.alert('消息', errorThrown);
				}
			});
		}

		function getroles(node, dd) {
			$.ajax({
				type : "post",
				url : "UserManage/GetRoles",
				//                dataType: "xml",
				data : {
					userid : node.userid
				},
				async : false,
				success : function(data) {
					$('#roleselect').combogrid('setValues', data.split(','));
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					$.messager.alert('消息', errorThrown);
				}
			});
		}

		function save() {
			if (!$('#ssec-form').form('validate')) {
				return;
			}
			var a = $('#t1').val();
			var c = $('#t3').val();
			if (c == '') {
				$.messager.alert('消息', '请输入登录名');
				return;
			}
			var d = $('#t4').val();
			if (d == '') {
				$.messager.alert('消息', '请输入密码');
				return;
			}
			var b = $('#t2').val();
			if (b == '') {
				$.messager.alert('消息', '请输入姓名');
				return;
			}
			var e = $('#t5').combobox('getValue');
			//var f = $('#t6').attr("checked");
			var f = '';
			var g = $('#t7').val();
			var roles = $('#roleselect').combogrid('getValues');
			if (roles == '') {
				$.messager.alert('消息', '请选择角色');
				return;
			}
			var rolesid = '';
			for (var i = 0; i < roles.length; i++) {
				rolesid = rolesid + roles[i] + ',';
			}
			rolesid = rolesid.substr(0, rolesid.length - 1);
			var dept = $('#userdept').combogrid('getValue');
			if (dept == '') {
				$.messager.alert('消息', '请选择机构');
				return;
			}
			var phone = $('#phone').val();
			//if (phone == '') {$.messager.alert('消息','请输入电话'); return; }
			var tel = $('#txtTel').val();
			var mail = $('#txtMail').val();
			$.ajax({
				type : "post",
				url : "UserManage/AddUser",
				//                dataType: "xml",
				data : {
					userid : a,
					loginname : c,
					pwd : d,
					username : b,
					disabled : e,
					superadmin : f,
					sortby : g,
					roles : rolesid,
					userdept : dept,
					phone : phone,
					tel : tel,
					mail : mail,
					czr : PUser[0].userid
				},
				success : function(data) {
					$.messager.alert('消息', data);
					if (data == '保存成功!') {
						$('#add').window('close');
						bindgrid();
					}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					$.messager.alert('消息', errorThrown);
				}
			});
		}
		function editrow() {
			//编辑用户
			var user = $('#dg').datagrid('getSelected');
			if (user) {
				$('#add').window('open');
				$('#t1').val(user.userid);
				$('#t2').val(unescape(user.username));
				$('#t4').val(user.pwd);
				//alert(unescape(user.PWD));
				$('#t3').val(unescape(user.loginname));
				$('#t5').combobox('setValue', user.disabled);
				//if (user.DISABLED == "1")
				//    $('#t5').attr('checked', true);
				//else
				//    $('#t5').attr('checked', false);
				//if (user.SUPERADMIN == "1")
				//    $('#t6').attr('checked', true);
				//else
				//    $('#t6').attr('checked', false);
				$('#t7').numberbox('setValue', user.usersort);
				$('#phone').val(user.phone);
				$('#txtTel').val(unescape(user.tel));
				$('#txtMail').val(user.mail);
				//combo tree grid
				$('#roleselect').combogrid('clear');
				console.log('dept:' + user.userdept);
				$('#userdept').combotree('setValue', user.userdept);
				//var sv = Xiaov.SmartWeb.Application.SysAdmin.Admin_User_CL.GetRolesIdByUserId(user.USERID).value;
				////$('#roleselect').combogrid('setValues', Xiaov.SmartWeb.Application.SysAdmin.Admin_User.GetRolesIdByUserId(user.USERID).value);
				var roles = getroles(user);
				//$('#roleselect').combogrid('setValues', roles.split(','));

				//$('#userdept').combogrid('clear');
			} else {
				$.messager.alert('消息', '请选中一个用户');
			}
		}
	</script>
	<script>
		function bindcombogrid() {
			$('#roleselect').combogrid(
					{
						multiple : true,
						panelWidth : 400,
						idField : 'roleid',
						textField : 'rolename',
						singleSelect : true,
						url : 'RoleManage/RoleListJson_combogrid?userid='
								+ PUser[0].userid,
						columns : [ [ {
							field : 'ck',
							checkbox : true
						}, {
							field : 'roleid',
							title : '角色编码',
							width : 10,
							hidden : true
						}, {
							field : 'rolename',
							title : '角色名称',
							width : 100,
							formatter : function(val, rec) {
								return unescape(val)
							}
						}
						//{ field: 'DISABLED', title: '禁用', width: 70, align: 'center', formatter: function (val, rec) { if (val == 0) return '否'; else return true; } },
						//{ field: 'ROLECOMMENT', title: '备注', width: 150, align: 'left', formatter: function (val, rec) { return unescape(val) } }
						] ],
						fitColumns : true
					});
		}
		function binduserdept() {
			$('#userdept').combotree(
					{
						url : 'DeptManage/DeptListJson_combobox?dwbm='
								+ PUser[0].userdept
					});
		}
		function bindtree() {
			$('#tt').treegrid({
				title : '组织机构列表',
				url : 'DeptManage/DeptListJson?dwbm=' + PUser[0].userdept,
				iconCls : 'icon-applicationgo',
				nowrap : true,
				width : 300,
				height : 500,
				rownumbers : true,
				animate : false,
				collapsible : false,
				singleSelect : true,
				remoteSort : false,
				idField : 'dwbm',
				treeField : 'dwmc',
				columns : [ [ {
					field : 'dwbm',
					title : '编码',
					width : 50,
					hidden : true
				}, {
					field : 'dwmc',
					title : '名称',
					sortable : true,
					width : 240,
					formatter : function(val, rec) {
						return unescape(val)
					}
				} ] ],
				onClickRow : function() {
					bindgrid();
				},
				onLoadSuccess : function() {
					$('#tt').treegrid('collapseAll');
					var rows = $('#tt').treegrid('getChildren');
					if (rows.length) {
						$('#tt').treegrid('expand', rows[0].dwbm);
					}
				}
			});
		}
		function bindgrid() {
			var dept;
			var node = $('#tt').treegrid('getSelected');
			if (node != null) {
				dept = node.dwbm;
			}
			$('#dg').datagrid(
					{
						title : '用户列表',
						url : 'UserManage/UserListJson?dwbm=' + dept,
						iconCls : 'icon-application',
						width : 700,
						height : 500,
						nowrap : true,
						rownumbers : true,
						animate : false,
						collapsible : false,
						singleSelect : true,
						remoteSort : false,
						columns : [ [ {
							field : 'username',
							title : '姓名',
							sortable : true,
							formatter : function(val, rec) {
								return unescape(val)
							}
						}, {
							field : 'loginname',
							title : '登录名',
							sortable : true,
							formatter : function(val, rec) {
								return unescape(val)
							}
						},
						//                             { field: 'PASSWORD', title: '密码', width: 100, formatter: function (val, rec) { return unescape(val) } },
						{
							field : 'disabled',
							title : '是否禁用',
							formatter : function(val, rec) {
								if (val == 0)
									return '否';
								else
									return '是';
							}
						},
						//                             { field: 'SUPERADMIN', title: '是否超管', width: 50, formatter: function (val, rec) { return unescape(val) } },
						{
							field : 'usersort',
							title : '排序',
							sortable : true,
							formatter : function(val, rec) {
								return unescape(val)
							}
						},
						//{
						//    field: 'USERID', title: '角色', width: 100, formatter: function (value) {
						//        return getRoleDescript(value);
						//    }
						//},
						{
							field : 'phone',
							title : '电话',
							sortable : true,
							formatter : function(val, rec) {
								return unescape(val)
							}
						}, {
							field : 'tel',
							title : '手机',
							sortable : true,
							formatter : function(val, rec) {
								return unescape(val)
							}
						}, {
							field : 'mail',
							title : '邮箱',
							sortable : true,
							formatter : function(val, rec) {
								return unescape(val)
							}
						} ] ],
						toolbar : [
								{
									text : '增加用户',
									iconCls : 'icon-add',
									handler : function() {
										//增加用户
										$('#ssec-form').form('clear');
										$('#add').window('open');
										var node = $('#tt').treegrid(
												'getSelected');
										if (node != null) {
											//alert(node.DWBM);
											$('#userdept').combotree(
													'setValue', node.dwbm);
										}
									}
								},
								'-',
								{
									text : '编辑用户',
									iconCls : 'icon-edit',
									handler : function() {
										editrow();
									}
								},
								'-',
								{
									text : '删除用户',
									iconCls : 'icon-cancel',
									handler : function() {
										//删除用户
										var user = $('#dg').datagrid(
												'getSelected');
										if (user) {
											$.messager.confirm('提示', '确定要删除【'
													+ unescape(user.USERNAME)
													+ '】这个用户吗?', function(r) {
												if (r) {
													del(user);
												}
											});
										}
									}
								} ],
						onLoadSuccess : function() {
						},
						onDblClickRow : function() {
							editrow();
						}
					});
		}
	</script>
	<style scoped="scoped">
.textbox {
	height: 20px;
	margin: 0;
	padding: 0 2px;
	box-sizing: content-box;
}

/*.table tr {
        height: 15px;
    }*/
</style>
	<table>
		<tr>
			<td>
				<table id="tt" style="height: 500px;"></table>
			</td>
			<td>
				<table id="dg"></table>
			</td>
		</tr>
	</table>
	<div id="add" class="easyui-window" closed="true" modal="true"
		title="用户信息" style="width: 500px; height: 500px;padding:10px 20px">
		<form id="ssec-form" class="ssec-form">
			<table class="table">
				<tr style="display: none;">
					<td>
						<div class="ssec-label" style="display: none;">用户ID：</div>
					</td>
					<td>
						<div class="ssec-text normal" style="display: none;">
							<input id="t1" type="text" />
						</div>
					</td>
				</tr>
				<tr>
					<td>
						<div class="ssec-label">登陆名：</div>
					</td>
					<td>
						<div class="ssec-text normal">
							<input id="t3" class="easyui-validatebox textbox"
								data-options="required:true" />
						</div>
					</td>
				</tr>
				<tr>
					<td>
						<div class="ssec-label">密码：</div>
					</td>
					<td>
						<div class="ssec-text normal">
							<input id="t4" type="password" class="easyui-validatebox textbox"
								data-options="required:true" />
						</div>
					</td>
				</tr>
				<tr>
					<td>
						<div class="ssec-label">姓名：</div>
					</td>
					<td>
						<div class="ssec-text normal">
							<input id="t2" class="easyui-validatebox textbox"
								data-options="required:true" />
						</div>
					</td>
				</tr>
				<tr>
					<td>
						<div class="ssec-label">启用禁用：</div>
					</td>
					<td><select id="t5" class="easyui-combobox"
						style="width:230px;">
							<option value="1">启用</option>
							<option value="0">禁用</option>
					</select></td>
				</tr>
				<!-- 				@* -->
				<!-- 				<tr style="display: none;"> -->
				<!-- 					<td> -->
				<!-- 						<div style="display: none;"> -->
				<!-- 							<div class="ssec-label">超管：</div> -->
				<!-- 						</div> -->
				<!-- 					</td> -->
				<!-- 					<td> -->
				<!-- 						<div style="display: none;"> -->
				<!-- 							<input id="t6" type="checkbox" /> -->
				<!-- 						</div> -->
				<!-- 					</td> -->
				<!-- 				</tr> -->
				<!-- 				*@ -->
				<tr>
					<td>
						<div class="ssec-label">角色：</div>
					</td>
					<td><input id="roleselect" class="easyui-combogrid"
						style="width: 230px" /></td>
				</tr>
				<tr>
					<td>
						<div class="ssec-label">所属机构：</div>
					</td>
					<td><input class="easyui-combotree" id="userdept"
						style="width: 230px" /></td>
				</tr>
				<tr>
					<td>
						<div class="ssec-label">排序：</div>
					</td>
					<td>
						<div class="ssec-text normal">
							<input id="t7" class="easyui-numberbox"
								data-options="required:true" />
						</div>
					</td>
				</tr>
				<tr>
					<td>
						<div class="ssec-label">电话：</div>
					</td>
					<td>
						<div class="ssec-text normal">
							<input id="phone" class="easyui-validatebox textbox" />
						</div>
					</td>
				</tr>
				<tr>
					<td>
						<div class="ssec-label">手机：</div>
					</td>
					<td>
						<div class="ssec-text normal">
							<input id="txtTel" class="easyui-validatebox textbox" />
						</div>
					</td>
				</tr>
				<tr>
					<td>
						<div class="ssec-label">邮箱：</div>
					</td>
					<td>
						<div class="ssec-text normal">
							<input id="txtMail" class="easyui-validatebox textbox" />
						</div>
					</td>
				</tr>
			</table>
			<div style="padding: 5px;text-align:right;">
				<a class="easyui-linkbutton" onclick="save();" iconcls="icon-save">保存</a>
			</div>
		</form>
	</div>
</body>
</html>
